package com.h3bpm.kumhosunny.knowledge.controller;

import OThinker.H3.Controller.ControllerBase;
import OThinker.H3.Entity.Notification.Notification;
import OThinker.H3.Entity.Notification.NotifyType;
import com.h3bpm.kumhosunny.common.utils.SqlUtil;
import data.DataRow;
import data.DataTable;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @authormi
 * @date2020/1/2
 */

@Component
public class WwwMessageSendController extends ControllerBase {
    @Override
    public String getFunctionCode() {
        return null;
    }




    @Scheduled(cron = "0 0 8 * * ?")
    public void WwwMessageSend() {


     String getDataSql = "select a.*,c.`Code`,c.`Name` as userName,c.ObjectID as userId,b.ObjectID as instanceId,b.InstanceName from i_todolist a  inner join ot_instanceContext b   on  a.ObjectID=b.BizObjectId inner join ot_user c on c.ObjectID=b.Originator ";


        try {

            Date newDate = new Date();
            SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd");
            DataTable dataTable = SqlUtil.doQuery(getDataSql);

            if(dataTable!=null && dataTable.getEntityRows()!=null && dataTable.getEntityRows().size()>0 ) {

                //循环当前3w跟进列表内容
                for(DataRow dataRow : dataTable.getEntityRows()) {
                    if (dataRow.getDate("Deadline01") != null) {
                        Date dateStr = dataRow.getDate("Deadline01");
                        String InstanceName = dataRow.getString("InstanceName");
                        if (newDate.getTime() >= dateStr.getTime()) {
                            //发送消息提醒
                            String instanceId = dataRow.getString("instanceId");
                            String getMessageWorkSql = "select  a.ObjectID,b.Code  from ot_workitem a  inner join  ot_user b on a.Participant = b.objectId where a.ActivityCode = 'Activity3' and  a.InstanceId =  '" + instanceId + "' ";
                            DataTable workDataTable = SqlUtil.doQuery(getMessageWorkSql);
                            if (workDataTable != null && workDataTable.getEntityRows() != null && workDataTable.getEntityRows().size() > 0) {
                                String Code = workDataTable.getEntityRows().get(0).getString("Code");
                                Notification notification = new Notification(NotifyType.DingTalk, null, null, Code, null, -1, instanceId, "审批:"+InstanceName, "您有一条流程未办理", null,
                                        "http://www.kumhosunny.cn:8099/Mobile/#/dingtalkmsg/" + workDataTable.getEntityRows().get(0).getString("ObjectID")+"?time="+System.currentTimeMillis(), "http://www.kumhosunny.cn:8099/Portal/template/ddhome.html?loginfrom=dingtalk&WorkItemID=" + workDataTable.getEntityRows().get(0).getString("ObjectID")+"&time="+System.currentTimeMillis(), null);
                                getEngine().getNotifier().Notify(notification);
                            }

                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }


    }



}
